    //
//  ThirdViewController.m
//  Login
//
//  Created by FER on 11/11/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "ThirdViewController.h"
#import "ConnectionHTTP.h"
#import "ReportDetail.h"

@implementation ThirdViewController

@synthesize reportsDetailsView;

ReportDetail *newViewReports;
NSMutableArray *listOfReports;
NSMutableArray *listOfReports_ID;

//---insert individual row into the table view---
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	static NSString *CellIdentifier = @"Cell";
	
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
	
	if (cell == nil) { 
		cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
									   reuseIdentifier:CellIdentifier] 
				autorelease];
	}
	NSString *cellValue = [listOfReports objectAtIndex:indexPath.row]; 
	cell.textLabel.text = cellValue;
	
	return cell;
	
}

//---set the number of rows in the table view---
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	return [listOfReports count];
}


 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
/*
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization.
    }
    return self;
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
	
	
	self.navigationItem.title = @"Lista de Reportes";
	
	listOfReports = [[NSMutableArray alloc] init];
	listOfReports_ID = [[NSMutableArray alloc] init];
	NSString *newsURL = [NSString stringWithFormat:@"ios/lastReports.php"];
	NSString *newsResponse= [ConnectionHTTP doRequest:(NSString *)newsURL];
	
	//Parse XML
	xmlBuffer = [ [ newsResponse stringByReplacingOccurrencesOfString: @"<" withString :@"\n<" ] 
				 componentsSeparatedByCharactersInSet: [ NSCharacterSet newlineCharacterSet ] ];
	
	for(int i = 0; i < [ xmlBuffer count ]; i++) 
	{
		NSString *line = [ xmlBuffer objectAtIndex: i ];
		if ([ line hasPrefix: @"<REPORT>" ]) 
		{
			NSString *id_report;
			NSString *title;
			NSString *date;
			
			while(!([ line isEqualToString: @"</REPORT>" ])) 
			{
				if ([ line hasPrefix: @"<REPORTID>" ]) {
					id_report  = [ line stringByReplacingOccurrencesOfString: @"<REPORTID>" withString: @"" ];		
				}
				if ([ line hasPrefix: @"<REPORTTITLE>" ]) 
				{
					title  = [ line stringByReplacingOccurrencesOfString: @"<REPORTTITLE>" withString: @"" ];		
				}
				if ([ line hasPrefix: @"<REPORTDATE>" ]) 
				{
					date  = [ line stringByReplacingOccurrencesOfString: @"<REPORTDATE>" withString: @"" ];		
				}
				i++;
				line = [ xmlBuffer objectAtIndex: i ];
			}
			NSString *line= [NSString stringWithFormat:@"%@  Fecha: %@",title,date];
			[listOfReports addObject:line]; 
			[listOfReports_ID addObject:id_report];
			NSLog(@"%@ ID: %@",line,id_report);
			
		}
	}
	 

    [super viewDidLoad];
}



-(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
	NSLog(@"didSelectRowAtIndexPath");
	int indexSelected = indexPath.row;
	NSInteger selected = indexSelected;
	IDdelReportSelected = [listOfReports_ID objectAtIndex:selected];
	
	
	//---add the view of the view controller to the current View---
	//---Navigate to the details view---
	if (reportsDetailsView == nil) 
	{ 
		NSString *title;
		NSString *description;
		NSString *date;
		NSString *coordinates;
		NSString *address;
		NSString *user;
		NSString *type;
		
		NSString *newsURL = [NSString stringWithFormat:@"ios/getReport.php?reportID=%@",IDdelReportSelected];
		//NSString *newsURL = [NSString stringWithFormat:@"http://inspiron15/ios/body.php"];
		NSString *newsResponse= [ConnectionHTTP doRequest:(NSString *)newsURL];
		NSLog(@"RESPONSE: %@",newsResponse);
		
		//Parse XML
		xmlBuffer = [ [ newsResponse stringByReplacingOccurrencesOfString: @"<" withString :@"\n<" ] componentsSeparatedByCharactersInSet: [ NSCharacterSet newlineCharacterSet ] ];
		
		for(int i = 0; i < [ xmlBuffer count ]; i++) 
		{
			NSString *line = [ xmlBuffer objectAtIndex: i ];
			
			if ([ line hasPrefix: @"<TITLE>" ]) {
				title  = [ line stringByReplacingOccurrencesOfString: @"<TITLE>" withString: @"" ];		
			}
			if ([ line hasPrefix: @"<DESCRIPTION>" ]) {
				description  = [ line stringByReplacingOccurrencesOfString: @"<DESCRIPTION>" withString: @"" ];		
			}
			if ([ line hasPrefix: @"<DATE>" ]) {
				date  = [ line stringByReplacingOccurrencesOfString: @"<DATE>" withString: @"" ];		
			}
			if ([ line hasPrefix: @"<COORDINATES>" ]) {
				coordinates  = [ line stringByReplacingOccurrencesOfString: @"<COORDINATES>" withString: @"" ];		
			}
			if ([ line hasPrefix: @"<ADDRESS>" ]) {
				address  = [ line stringByReplacingOccurrencesOfString: @"<ADDRESS>" withString: @"" ];		
			}
			if ([ line hasPrefix: @"<USER>" ]) {
				user  = [ line stringByReplacingOccurrencesOfString: @"<USER>" withString: @"" ];		
			}
			if ([ line hasPrefix: @"<TYPE>" ]) {
				type  = [ line stringByReplacingOccurrencesOfString: @"<TYPE>" withString: @"" ];		
			}
		}
		
		//Nueva pantalla.
		newViewReports = [[ReportDetail alloc] initWithNibName:@"ReportDetail" bundle:[NSBundle mainBundle]];
		[self.view addSubview:newViewReports.view];
		
		[newViewReports.labelTitle setText:title];
		[newViewReports.labelDescription setText:description];
		[newViewReports.labelDate setText:date];
		[newViewReports.labelCoordinates setText:coordinates];
		[newViewReports.labelAddress setText:address];
		[newViewReports.labelUser setText:user];
		[newViewReports.labelType setText:type];
		
	}
}





- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
	//---display “Movie List” as the header---
	return @"Lista de reportes";
}
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section {
	//---display “by Denzel Washington” as the footer---
	return @"Mostrados los ultimos 10 reportes";
}



- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Overriden to allow any orientation.
    return YES;
}


- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}


- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
	[newViewReports release];
	[listOfReports release];
	[listOfReports_ID release];
    [super dealloc];
}


@end
